2 * Matt McCutchen's Big Integer Library
5 #include "BigInteger.hh"
10 void BigInteger::operator =(const BigInteger
&x
) {
11 // Calls like a = a have no effect
17 BigUnsigned::operator =(x
);
20 // Constructor from an array of blocks and a sign
21 BigInteger::BigInteger(const Blk
*b
, Index l
, Sign s
) : BigUnsigned(b
, l
) {
26 sign
= (len
== 0) ? zero
: s
;
29 throw "BigInteger::BigInteger(Blk *, Index, Sign): Invalid sign";
33 // Constructor from a BigUnsigned and a sign
34 BigInteger::BigInteger(const BigUnsigned
&x
, Sign s
) : BigUnsigned(x
) {
39 sign
= (len
== 0) ? zero
: s
;
42 throw "BigInteger::BigInteger(Blk *, Index, Sign): Invalid sign";
47 * The steps for construction of a BigInteger
48 * from an integral value x are as follows:
49 * 1. If x is zero, create an empty BigInteger and stop.
50 * 2. Allocate a one-block number array.
51 * 3. If x is positive (or of an unsigned type), set the
52 * sign of the BigInteger to positive.
53 * 4. If x is of a signed type and is negative, set the
54 * sign of the BigInteger to negative.
55 * 5. If x is of a signed type, convert x (or -x if x < 0)
56 * to the unsigned type of the same length.
57 * 6. Expand x (or the result of step 5) to a Blk,
58 * and store it in the number array.
60 * See remarks in `BigUnsigned.cc' and `NumberlikeArray.hh'
61 * about new handling of zero-length arrays.
64 BigInteger::BigInteger(unsigned long x
) {
66 sign
= zero
; // NumberlikeArray did the rest
76 BigInteger::BigInteger(long x
) {
93 BigInteger::BigInteger(unsigned int x
) {
105 BigInteger::BigInteger(int x
) {
122 BigInteger::BigInteger(unsigned short x
) {
134 BigInteger::BigInteger(short x
) {
153 * The steps for conversion of a BigInteger to an
154 * integral type are as follows:
155 * 1. If the BigInteger is zero, return zero.
156 * 2. If the BigInteger is positive:
157 * 3. If it is more than one block long or its lowest
158 * block has bits set out of the range of the target
159 * type, throw an exception.
160 * 4. Otherwise, convert the lowest block to the
161 * target type and return it.
162 * 5. If the BigInteger is negative:
163 * 6. If the target type is unsigned, throw an exception.
164 * 7. If it is more than one block long or its lowest
165 * block has bits set out of the range of the target
166 * type, throw an exception.
167 * 8. Otherwise, convert the lowest block to the
168 * target type, negate it, and return it.
172 // These masks are used to test whether a Blk has bits
173 // set out of the range of a smaller integral type. Note
174 // that this range is not considered to include the sign bit.
175 const BigUnsigned::Blk lMask
= ~0 >> 1;
176 const BigUnsigned::Blk uiMask
= (unsigned int)(~0);
177 const BigUnsigned::Blk iMask
= uiMask
>> 1;
178 const BigUnsigned::Blk usMask
= (unsigned short)(~0);
179 const BigUnsigned::Blk sMask
= usMask
>> 1;
182 BigInteger::operator unsigned long() const {
190 throw "BigInteger operator unsigned long() const: Value is too big for an unsigned long";
192 throw "BigInteger operator unsigned long() const: Cannot convert a negative integer to an unsigned type";
194 throw "BigInteger: Internal error";
198 BigInteger::operator long() const {
203 if (len
== 1 && (blk
[0] & ~lMask
) == 0)
206 throw "BigInteger operator long() const: Value is too big for a long";
208 if (len
== 1 && (blk
[0] & ~lMask
) == 0)
209 return -long(blk
[0]);
211 throw "BigInteger operator long() const: Value is too big for a long";
213 throw "BigInteger: Internal error";
217 BigInteger::operator unsigned int() const {
222 if (len
== 1 && (blk
[0] & ~uiMask
) == 0)
223 return (unsigned int)(blk
[0]);
225 throw "BigInteger operator unsigned int() const: Value is too big for an unsigned int";
227 throw "BigInteger operator unsigned int() const: Cannot convert a negative integer to an unsigned type";
229 throw "BigInteger: Internal error";
233 BigInteger::operator int() const {
238 if (len
== 1 && (blk
[0] & ~iMask
) == 0)
241 throw "BigInteger operator int() const: Value is too big for an int";
243 if (len
== 1 && (blk
[0] & ~iMask
) == 0)
246 throw "BigInteger operator int() const: Value is too big for an int";
248 throw "BigInteger: Internal error";
252 BigInteger::operator unsigned short() const {
257 if (len
== 1 && (blk
[0] & ~usMask
) == 0)
258 return (unsigned short)(blk
[0]);
260 throw "BigInteger operator unsigned short() const: Value is too big for an unsigned short";
262 throw "BigInteger operator unsigned short() const: Cannot convert a negative integer to an unsigned type";
264 throw "BigInteger: Internal error";
268 BigInteger::operator short() const {
273 if (len
== 1 && (blk
[0] & ~sMask
) == 0)
274 return short(blk
[0]);
276 throw "BigInteger operator short() const: Value is too big for a short";
278 if (len
== 1 && (blk
[0] & ~sMask
) == 0)
279 return -short(blk
[0]);
281 throw "BigInteger operator short() const: Value is too big for a short";
283 throw "BigInteger: Internal error";
288 BigInteger::CmpRes
BigInteger::compareTo(const BigInteger
&x
) const {
289 // A greater sign implies a greater number
292 else if (sign
> x
.sign
)
295 // If the signs are the same...
297 return equal
; // Two zeros are equal
299 // Compare the magnitudes
300 return BigUnsigned::compareTo(x
);
302 // Compare the magnitudes, but return the opposite result
303 return CmpRes(-BigUnsigned::compareTo(x
));
305 throw "BigInteger: Internal error";
309 // PUT-HERE OPERATIONS
310 // These do some messing around to determine the sign of the result,
311 // then call one of BigUnsigned's put-heres.
313 // See remarks about aliased calls in BigUnsigned.cc .
314 #define DOTR_ALIASED(cond, op) \
316 BigInteger tmpThis; \
323 void BigInteger::add(const BigInteger
&a
, const BigInteger
&b
) {
324 DOTR_ALIASED(this == &a
|| this == &b
, add(a
, b
));
325 // If one argument is zero, copy the other.
328 else if (b
.sign
== zero
)
330 // If the arguments have the same sign, take the
331 // common sign and add their magnitudes.
332 else if (a
.sign
== b
.sign
) {
334 BigUnsigned::add(a
, b
);
336 // Otherwise, their magnitudes must be compared.
337 switch (a
.BigUnsigned::compareTo(b
)) {
338 // If their magnitudes are the same, copy zero.
343 // Otherwise, take the sign of the greater, and subtract
344 // the lesser magnitude from the greater magnitude.
347 BigUnsigned::subtract(a
, b
);
351 BigUnsigned::subtract(b
, a
);
358 void BigInteger::subtract(const BigInteger
&a
, const BigInteger
&b
) {
359 // Notice that this routine is identical to BigInteger::add,
360 // if one replaces b.sign by its opposite.
361 DOTR_ALIASED(this == &a
|| this == &b
, subtract(a
, b
));
362 // If a is zero, copy b and flip its sign. If b is zero, copy a.
363 if (a
.sign
== zero
) {
364 BigUnsigned::operator =(b
);
365 // Take the negative of _b_'s, sign, not ours.
366 // Bug pointed out by Sam Larkin on 2005.03.30.
367 sign
= Sign(-b
.sign
);
368 } else if (b
.sign
== zero
)
370 // If their signs differ, take a.sign and add the magnitudes.
371 else if (a
.sign
!= b
.sign
) {
373 BigUnsigned::add(a
, b
);
375 // Otherwise, their magnitudes must be compared.
376 switch (a
.BigUnsigned::compareTo(b
)) {
377 // If their magnitudes are the same, copy zero.
382 // If a's magnitude is greater, take a.sign and
383 // subtract a from b.
386 BigUnsigned::subtract(a
, b
);
388 // If b's magnitude is greater, take the opposite
389 // of b.sign and subtract b from a.
391 sign
= Sign(-b
.sign
);
392 BigUnsigned::subtract(b
, a
);
399 void BigInteger::multiply(const BigInteger
&a
, const BigInteger
&b
) {
400 DOTR_ALIASED(this == &a
|| this == &b
, multiply(a
, b
));
401 // If one object is zero, copy zero and return.
402 if (a
.sign
== zero
|| b
.sign
== zero
) {
407 // If the signs of the arguments are the same, the result
408 // is positive, otherwise it is negative.
409 sign
= (a
.sign
== b
.sign
) ? positive
: negative
;
410 // Multiply the magnitudes.
411 BigUnsigned::multiply(a
, b
);
415 * DIVISION WITH REMAINDER
416 * Please read the comments before the definition of
417 * `BigUnsigned::divideWithRemainder' in `BigUnsigned.cc' for lots of
418 * information you should know before reading this function.
420 * Following Knuth, I decree that x / y is to be
421 * 0 if y==0 and floor(real-number x / y) if y!=0.
422 * Then x % y shall be x - y*(integer x / y).
424 * Note that x = y * (x / y) + (x % y) always holds.
425 * In addition, (x % y) is from 0 to y - 1 if y > 0,
426 * and from -(|y| - 1) to 0 if y < 0. (x % y) = x if y = 0.
428 * Examples: (q = a / b, r = a % b)
436 void BigInteger::divideWithRemainder(const BigInteger
&b
, BigInteger
&q
) {
437 // Defend against aliased calls;
438 // same idea as in BigUnsigned::divideWithRemainder .
440 throw "BigInteger::divideWithRemainder: Cannot write quotient and remainder into the same variable";
441 if (this == &b
|| &q
== &b
) {
443 divideWithRemainder(tmpB
, q
);
447 // Division by zero gives quotient 0 and remainder *this
448 if (b
.sign
== zero
) {
453 // 0 / b gives quotient 0 and remainder 0
460 // Here *this != 0, b != 0.
462 // Do the operands have the same sign?
463 if (sign
== b
.sign
) {
464 // Yes: easy case. Quotient is zero or positive.
467 // No: harder case. Quotient is negative.
469 // Decrease the magnitude of the dividend by one.
470 BigUnsigned::operator --();
472 * We tinker with the dividend before and with the
473 * quotient and remainder after so that the result
474 * comes out right. To see why it works, consider the following
475 * list of examples, where A is the magnitude-decreased
476 * a, Q and R are the results of BigUnsigned division
477 * with remainder on A and |b|, and q and r are the
478 * final results we want:
486 * It appears that we need a total of 3 corrections:
487 * Decrease the magnitude of a to get A. Increase the
488 * magnitude of Q to get q (and make it negative).
489 * Find r = (b - 1) - R and give it the desired sign.
493 // Divide the magnitudes.
494 BigUnsigned::divideWithRemainder(b
, q
);
496 if (sign
!= b
.sign
) {
497 // More for the harder case (as described):
498 // Increase the magnitude of the quotient by one.
499 q
.BigUnsigned::operator ++();
500 // Modify the remainder.
501 BigUnsigned
temp(*this);
502 BigUnsigned::subtract(b
, temp
);
503 BigUnsigned::operator --();
506 // Sign of the remainder is always the sign of the divisor b.
509 // Set signs to zero as necessary. (Thanks David Allen!)
519 void BigInteger::negate(const BigInteger
&a
) {
520 DOTR_ALIASED(this == &a
, negate(a
));
521 // Copy a's magnitude
522 BigUnsigned::operator =(a
);
523 // Copy the opposite of a.sign
524 sign
= Sign(-a
.sign
);
527 // INCREMENT/DECREMENT OPERATORS
530 void BigInteger::operator ++() {
539 BigUnsigned::operator ++();
542 BigUnsigned::operator --();
549 // Postfix increment: same as prefix
550 void BigInteger::operator ++(int) {
555 void BigInteger::operator --() {
564 BigUnsigned::operator ++();
567 BigUnsigned::operator --();
574 // Postfix decrement: same as prefix
575 void BigInteger::operator --(int) {